package cn.lyq.card.ui.achievement

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import cn.lyq.card.R
import cn.lyq.card.base.BaseDialogFg
import cn.lyq.card.global.BusTag
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.BusUtils
import kotlinx.android.synthetic.main.dialog_date_select.*
import java.util.*


class DateSelectFg : BaseDialogFg() {

    companion object {
        private const val KEY_TYPE = "TYPE"
        private const val KEY_HAS_DAY = "HAS_DAY"
        private const val KEY_DATE = "DATE"

        fun newInstance(type: Int, hasDay: Boolean, date: Calendar): DateSelectFg {
            val args = Bundle()
            args.putInt(KEY_TYPE, type)
            args.putBoolean(KEY_HAS_DAY, hasDay)
            args.putSerializable(KEY_DATE, date)
            val fragment = DateSelectFg()
            fragment.arguments = args
            return fragment
        }
    }

    override val rootId: Int
        get() = R.layout.dialog_date_select
    private val type by lazy {
        arguments?.getInt(KEY_TYPE, 0) ?: 0
    }
    private val hasDay by lazy {
        arguments?.getBoolean(KEY_HAS_DAY, true) ?: true
    }
    private val date by lazy {
        val temp = arguments?.getSerializable(KEY_DATE)
        if (temp is Calendar) {
            temp
        } else {
            Calendar.getInstance()
        }
    }

    override fun initView(view: View) {
        super.initView(view)

        dpv.run {
            setShowLabel(false)
            setCurved(false)
            setTextSize(resources.getDimensionPixelOffset(R.dimen.font17).toFloat())
            setDrawSelectedRect(true)
            setMaxDate(Calendar.getInstance().time)
            if (!hasDay) {
                hideDayItem()
            }
            for (wheelView in arrayOf(yearWv, monthWv, dayWv)) {
                val params = wheelView.layoutParams as LinearLayout.LayoutParams
                val widthResId = if (hasDay) R.dimen.select_dialog_item_width_three else
                    R.dimen.select_dialog_item_width_two
                params.width = resources.getDimensionPixelOffset(widthResId)
                params.marginStart = 0
                params.marginEnd = 0
                wheelView.layoutParams = params
            }
            setSelectedRectColor(Color.parseColor("#F0F4FC"))
            setSelectedItemTextColorRes(R.color.font_blue)
            setNormalItemTextColorRes(R.color.font_gray)
            yearWv.setIntegerNeedFormat("%02d年")
            monthWv.setIntegerNeedFormat("%02d月")
            dayWv.setIntegerNeedFormat("%02d日")
            selectedYear = date.get(Calendar.YEAR)
            selectedMonth = date.get(Calendar.MONTH) + 1
            selectedDay = date.get(Calendar.DAY_OF_MONTH)
            setLineSpacing(resources.getDimensionPixelOffset(R.dimen.font17).toFloat())
        }
    }

    override fun initListener(view: View) {
        super.initListener(view)
        tvConfirm.setClick {
            val dateString = if (hasDay) {
                "${dpv.selectedYear}-${dpv.selectedMonth.toDay()}-${dpv.selectedDay.toDay()}"
            } else {
                "${dpv.selectedYear}-${dpv.selectedMonth.toDay()}"
            }
            date.set(dpv.selectedYear, dpv.selectedMonth - 1, dpv.selectedDay)
            BusUtils.post(BusTag.DATE_SELECT, Triple(type, dateString, date.timeInMillis))
            dismiss()
        }
        tvCancel.setClick {
            dismiss()
        }
    }

    private fun Int.toDay(): String {
        return if (this < 10) {
            "0${this}"
        } else {
            this.toString()
        }
    }
}